{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [numpy quick start](https://numpy.org/doc/stable/user/quickstart.html#the-basics)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T12:56:17.621657Z",
     "start_time": "2020-12-02T12:56:17.616568Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:29:08.042961Z",
     "start_time": "2020-12-01T06:29:08.040206Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.arange(15).reshape(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:29:10.169917Z",
     "start_time": "2020-12-01T06:29:10.160430Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:29:13.564215Z",
     "start_time": "2020-12-01T06:29:13.560304Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 5)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:29:22.994096Z",
     "start_time": "2020-12-01T06:29:22.990578Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:29:35.724549Z",
     "start_time": "2020-12-01T06:29:35.720757Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'int64'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:29:40.315951Z",
     "start_time": "2020-12-01T06:29:40.312466Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.itemsize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:29:45.359207Z",
     "start_time": "2020-12-01T06:29:45.355640Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:29:49.987682Z",
     "start_time": "2020-12-01T06:29:49.984256Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:30:09.946543Z",
     "start_time": "2020-12-01T06:30:09.944045Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.array([6,7,8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:30:13.171588Z",
     "start_time": "2020-12-01T06:30:13.168104Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:31:19.708225Z",
     "start_time": "2020-12-01T06:31:19.705541Z"
    }
   },
   "outputs": [],
   "source": [
    "c = np.array([[1,2], [3,4]], dtype=complex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:31:21.404291Z",
     "start_time": "2020-12-01T06:31:21.400419Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.+0.j, 2.+0.j],\n",
       "       [3.+0.j, 4.+0.j]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:31:54.981015Z",
     "start_time": "2020-12-01T06:31:54.976898Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:32:11.069697Z",
     "start_time": "2020-12-01T06:32:11.064778Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 1, 1, 1],\n",
       "        [1, 1, 1, 1],\n",
       "        [1, 1, 1, 1]],\n",
       "\n",
       "       [[1, 1, 1, 1],\n",
       "        [1, 1, 1, 1],\n",
       "        [1, 1, 1, 1]]], dtype=int16)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((2,3,4), dtype=np.int16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:32:50.929636Z",
     "start_time": "2020-12-01T06:32:50.925715Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.empty((2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:33:07.164335Z",
     "start_time": "2020-12-01T06:33:07.160180Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 15, 20, 25])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10, 30, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:33:43.580480Z",
     "start_time": "2020-12-01T06:33:43.576203Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0, 2, 0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:34:29.554939Z",
     "start_time": "2020-12-01T06:34:29.552563Z"
    }
   },
   "outputs": [],
   "source": [
    "from numpy import pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:34:38.244795Z",
     "start_time": "2020-12-01T06:34:38.240200Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  ])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0,2,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:35:14.586896Z",
     "start_time": "2020-12-01T06:35:14.584303Z"
    }
   },
   "outputs": [],
   "source": [
    "np.linspace(0, 2*pi, 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:35:36.507449Z",
     "start_time": "2020-12-01T06:35:36.504091Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]]\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  基本操作\n",
    "算术操作按位运算，新的array被创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:39:21.530921Z",
     "start_time": "2020-12-01T06:39:21.528523Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.array([20, 30, 40, 50])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:39:26.683067Z",
     "start_time": "2020-12-01T06:39:26.680572Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.arange(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:39:32.346905Z",
     "start_time": "2020-12-01T06:39:32.344508Z"
    }
   },
   "outputs": [],
   "source": [
    "c = a-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:39:34.820045Z",
     "start_time": "2020-12-01T06:39:34.816012Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, 29, 38, 47])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:39:41.995808Z",
     "start_time": "2020-12-01T06:39:41.991977Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 4, 9])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:39:49.821222Z",
     "start_time": "2020-12-01T06:39:49.816563Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 * np.sin(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:39:53.715470Z",
     "start_time": "2020-12-01T06:39:53.711858Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True, False, False])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a < 35"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 乘法(*)运算按位运算，矩阵乘法用@或者dot函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:41:15.236676Z",
     "start_time": "2020-12-01T06:41:15.233865Z"
    }
   },
   "outputs": [],
   "source": [
    "A = np.array([[1,1], [0,1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:41:36.442627Z",
     "start_time": "2020-12-01T06:41:36.440020Z"
    }
   },
   "outputs": [],
   "source": [
    "B = np.array([[2,0], [3,4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:41:39.563735Z",
     "start_time": "2020-12-01T06:41:39.559972Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 0],\n",
       "       [0, 4]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A * B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:41:43.675986Z",
     "start_time": "2020-12-01T06:41:43.671979Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 4],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:41:59.083637Z",
     "start_time": "2020-12-01T06:41:59.079797Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 4],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.dot(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### += 和 *= 操作会原地替换array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:44:29.875373Z",
     "start_time": "2020-12-01T06:44:29.871852Z"
    }
   },
   "outputs": [],
   "source": [
    "rg = np.random.default_rng(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:44:53.914506Z",
     "start_time": "2020-12-01T06:44:53.911755Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.ones((2,3), dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:45:08.618859Z",
     "start_time": "2020-12-01T06:45:08.615626Z"
    }
   },
   "outputs": [],
   "source": [
    "b = rg.random((2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:45:28.834350Z",
     "start_time": "2020-12-01T06:45:28.831921Z"
    }
   },
   "outputs": [],
   "source": [
    "a *= 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:45:31.980906Z",
     "start_time": "2020-12-01T06:45:31.976267Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 3, 3],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:45:41.074350Z",
     "start_time": "2020-12-01T06:45:41.071785Z"
    }
   },
   "outputs": [],
   "source": [
    "b += a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:45:41.811464Z",
     "start_time": "2020-12-01T06:45:41.807503Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.51182162, 3.9504637 , 3.14415961],\n",
       "       [3.94864945, 3.31183145, 3.42332645]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 不同数据类型运算，向上转型 upcasting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:46:26.517791Z",
     "start_time": "2020-12-01T06:46:26.400381Z"
    }
   },
   "outputs": [
    {
     "ename": "UFuncTypeError",
     "evalue": "Cannot cast ufunc 'add' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mUFuncTypeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-48-294cacd62d6f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mUFuncTypeError\u001b[0m: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'"
     ]
    }
   ],
   "source": [
    "a += b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:47:38.874946Z",
     "start_time": "2020-12-01T06:47:38.872296Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.ones(3, dtype=np.int32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:47:55.178559Z",
     "start_time": "2020-12-01T06:47:55.175746Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.linspace(0, pi, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:47:59.611570Z",
     "start_time": "2020-12-01T06:47:59.607941Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'float64'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.dtype.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:05.002144Z",
     "start_time": "2020-12-01T06:48:04.999769Z"
    }
   },
   "outputs": [],
   "source": [
    "c = a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:07.915378Z",
     "start_time": "2020-12-01T06:48:07.911735Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'float64'"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.dtype.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:16.978812Z",
     "start_time": "2020-12-01T06:48:16.975907Z"
    }
   },
   "outputs": [],
   "source": [
    "d = np.exp(c * 1j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:17.707630Z",
     "start_time": "2020-12-01T06:48:17.703692Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,\n",
       "       -0.54030231-0.84147098j])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:21.467480Z",
     "start_time": "2020-12-01T06:48:21.463829Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'complex128'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.dtype.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:41.714183Z",
     "start_time": "2020-12-01T06:48:41.711760Z"
    }
   },
   "outputs": [],
   "source": [
    "a = rg.random((2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:43.284388Z",
     "start_time": "2020-12-01T06:48:43.280382Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.82770259, 0.40919914, 0.54959369],\n",
       "       [0.02755911, 0.75351311, 0.53814331]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:47.499896Z",
     "start_time": "2020-12-01T06:48:47.495863Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.1057109529998157"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:51.436379Z",
     "start_time": "2020-12-01T06:48:51.432733Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.027559113243068367"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:48:53.059674Z",
     "start_time": "2020-12-01T06:48:53.055815Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8277025938204418"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:49:17.810310Z",
     "start_time": "2020-12-01T06:49:17.807869Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:49:20.115625Z",
     "start_time": "2020-12-01T06:49:20.111879Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:51:17.348201Z",
     "start_time": "2020-12-01T06:51:17.344206Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12, 15, 18, 21])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.sum(axis = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:51:28.180100Z",
     "start_time": "2020-12-01T06:51:28.176318Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 4, 8])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.min(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:52:00.195619Z",
     "start_time": "2020-12-01T06:52:00.191776Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  6,  8, 10],\n",
       "       [12, 15, 18, 21]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.cumsum(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引、切片和迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:53:12.322211Z",
     "start_time": "2020-12-01T06:53:12.319629Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.arange(10) ** 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:53:15.459238Z",
     "start_time": "2020-12-01T06:53:15.455667Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:53:35.419981Z",
     "start_time": "2020-12-01T06:53:35.416159Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:53:37.987874Z",
     "start_time": "2020-12-01T06:53:37.983931Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 8, 27, 64])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:54:35.338194Z",
     "start_time": "2020-12-01T06:54:35.335584Z"
    }
   },
   "outputs": [],
   "source": [
    "a[:6:2] = 1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:54:38.635386Z",
     "start_time": "2020-12-01T06:54:38.631712Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1000,    1, 1000,   27, 1000,  125,  216,  343,  512,  729])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:57:31.075410Z",
     "start_time": "2020-12-01T06:57:31.071445Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 729,  512,  343,  216,  125, 1000,   27, 1000,    1, 1000])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:57:52.404408Z",
     "start_time": "2020-12-01T06:57:52.399576Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9.999999999999998\n",
      "1.0\n",
      "9.999999999999998\n",
      "3.0\n",
      "9.999999999999998\n",
      "4.999999999999999\n",
      "5.999999999999999\n",
      "6.999999999999999\n",
      "7.999999999999999\n",
      "8.999999999999998\n"
     ]
    }
   ],
   "source": [
    "for i in a:\n",
    "    print(i ** (1/3.))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:58:26.154068Z",
     "start_time": "2020-12-01T06:58:26.151639Z"
    }
   },
   "outputs": [],
   "source": [
    "def f(x,y):\n",
    "    return 10*x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T06:58:42.459335Z",
     "start_time": "2020-12-01T06:58:42.456199Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.fromfunction(f, (5,4), dtype=int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:04:08.235688Z",
     "start_time": "2020-12-01T07:04:08.231670Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [10, 11, 12, 13],\n",
       "       [20, 21, 22, 23],\n",
       "       [30, 31, 32, 33],\n",
       "       [40, 41, 42, 43]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:03:25.212225Z",
     "start_time": "2020-12-01T07:03:25.207813Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 1., 2.],\n",
       "       [1., 2., 3.]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.fromfunction(lambda i,j : i + j ,(2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:03:44.202736Z",
     "start_time": "2020-12-01T07:03:44.199113Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "23"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:04:28.386943Z",
     "start_time": "2020-12-01T07:04:28.383026Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 11, 21, 31, 41])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[:, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:04:59.739821Z",
     "start_time": "2020-12-01T07:04:59.735988Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 11, 12, 13],\n",
       "       [20, 21, 22, 23]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1:3, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:05:14.482860Z",
     "start_time": "2020-12-01T07:05:14.479135Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([40, 41, 42, 43])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### dots (...) 代表尽可能多的: 产生一个完整的元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:11:02.154235Z",
     "start_time": "2020-12-01T07:11:02.151360Z"
    }
   },
   "outputs": [],
   "source": [
    "c = np.array( [[[  0,  1,  2],[ 10, 12, 13]],[[100,101,102],[110,112,113]]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:11:12.930871Z",
     "start_time": "2020-12-01T07:11:12.927124Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 3)"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:11:20.946898Z",
     "start_time": "2020-12-01T07:11:20.942933Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[  0,   1,   2],\n",
       "        [ 10,  12,  13]],\n",
       "\n",
       "       [[100, 101, 102],\n",
       "        [110, 112, 113]]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:11:28.146702Z",
     "start_time": "2020-12-01T07:11:28.143034Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[100, 101, 102],\n",
       "       [110, 112, 113]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c[1,...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:11:37.050682Z",
     "start_time": "2020-12-01T07:11:37.046795Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  2,  13],\n",
       "       [102, 113]])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c[...,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:13:02.956793Z",
     "start_time": "2020-12-01T07:13:02.951093Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "20\n",
      "21\n",
      "22\n",
      "23\n",
      "30\n",
      "31\n",
      "32\n",
      "33\n",
      "40\n",
      "41\n",
      "42\n",
      "43\n"
     ]
    }
   ],
   "source": [
    "for row in b.flat:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 形状操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:15:56.441846Z",
     "start_time": "2020-12-01T07:15:56.439017Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.floor(10 * rg.random((3,4)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:15:58.916038Z",
     "start_time": "2020-12-01T07:15:58.911912Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3., 7., 3., 4.],\n",
       "       [1., 4., 2., 2.],\n",
       "       [7., 2., 4., 9.]])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:16:27.948128Z",
     "start_time": "2020-12-01T07:16:27.943831Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 7., 3., 4., 1., 4., 2., 2., 7., 2., 4., 9.])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:16:35.244247Z",
     "start_time": "2020-12-01T07:16:35.240269Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3., 7.],\n",
       "       [3., 4.],\n",
       "       [1., 4.],\n",
       "       [2., 2.],\n",
       "       [7., 2.],\n",
       "       [4., 9.]])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(6,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:16:40.626411Z",
     "start_time": "2020-12-01T07:16:40.622564Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3., 1., 7.],\n",
       "       [7., 4., 2.],\n",
       "       [3., 2., 4.],\n",
       "       [4., 2., 9.]])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:16:55.451237Z",
     "start_time": "2020-12-01T07:16:55.447211Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 3)"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.T.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:16:57.410970Z",
     "start_time": "2020-12-01T07:16:57.407192Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### reshape创建新array，resize改变自己"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:18:22.817067Z",
     "start_time": "2020-12-01T07:18:22.814536Z"
    }
   },
   "outputs": [],
   "source": [
    "a.resize((2,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:18:25.194502Z",
     "start_time": "2020-12-01T07:18:25.190625Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3., 7., 3., 4., 1., 4.],\n",
       "       [2., 2., 7., 2., 4., 9.]])"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:19:16.938950Z",
     "start_time": "2020-12-01T07:19:16.934664Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3., 7., 3., 4.],\n",
       "       [1., 4., 2., 2.],\n",
       "       [7., 2., 4., 9.]])"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(3, -1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 堆叠不同的array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:20:04.841489Z",
     "start_time": "2020-12-01T07:20:04.838892Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.floor(10 * rg.random((2,2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:20:06.314871Z",
     "start_time": "2020-12-01T07:20:06.311041Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9., 7.],\n",
       "       [5., 2.]])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:20:59.761165Z",
     "start_time": "2020-12-01T07:20:59.758719Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.floor(10*rg.random((2,2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:21:06.538369Z",
     "start_time": "2020-12-01T07:21:06.534763Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6., 8.],\n",
       "       [5., 2.]])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:21:15.210687Z",
     "start_time": "2020-12-01T07:21:15.206705Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9., 7.],\n",
       "       [5., 2.],\n",
       "       [6., 8.],\n",
       "       [5., 2.]])"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:21:25.035894Z",
     "start_time": "2020-12-01T07:21:25.031681Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9., 7., 6., 8.],\n",
       "       [5., 2., 5., 2.]])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:27:16.002350Z",
     "start_time": "2020-12-01T07:27:15.999891Z"
    }
   },
   "outputs": [],
   "source": [
    "from numpy import newaxis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:27:24.587776Z",
     "start_time": "2020-12-01T07:27:24.583198Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9., 7., 6., 8.],\n",
       "       [5., 2., 5., 2.]])"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.column_stack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:27:54.984743Z",
     "start_time": "2020-12-01T07:27:54.982356Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.array([4., 2.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:28:03.937192Z",
     "start_time": "2020-12-01T07:28:03.934702Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.array([3., 8.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:28:12.562617Z",
     "start_time": "2020-12-01T07:28:12.558583Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4., 3.],\n",
       "       [2., 8.]])"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.column_stack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:28:28.866409Z",
     "start_time": "2020-12-01T07:28:28.862432Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4., 2., 3., 8.])"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:28:54.402071Z",
     "start_time": "2020-12-01T07:28:54.398461Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.],\n",
       "       [2.]])"
      ]
     },
     "execution_count": 147,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:, newaxis]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:35:14.146279Z",
     "start_time": "2020-12-01T07:35:14.141840Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4., 3.],\n",
       "       [2., 8.]])"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.column_stack((a[:, newaxis], b[: ,newaxis]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:35:30.338439Z",
     "start_time": "2020-12-01T07:35:30.334422Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4., 3.],\n",
       "       [2., 8.]])"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a[:,newaxis],b[:,newaxis]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 分割array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:36:58.488652Z",
     "start_time": "2020-12-01T07:36:58.485773Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.floor(10 * rg.random((2,12)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:37:00.129724Z",
     "start_time": "2020-12-01T07:37:00.125709Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8., 5., 5., 7., 1., 8., 6., 7., 1., 8., 1., 0.],\n",
       "       [8., 8., 8., 4., 2., 0., 6., 7., 8., 2., 2., 6.]])"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:37:09.570661Z",
     "start_time": "2020-12-01T07:37:09.565748Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[8., 5., 5., 7.],\n",
       "        [8., 8., 8., 4.]]),\n",
       " array([[1., 8., 6., 7.],\n",
       "        [2., 0., 6., 7.]]),\n",
       " array([[1., 8., 1., 0.],\n",
       "        [8., 2., 2., 6.]])]"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:37:56.027284Z",
     "start_time": "2020-12-01T07:37:56.022247Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[8., 5., 5.],\n",
       "        [8., 8., 8.]]),\n",
       " array([[7.],\n",
       "        [4.]]),\n",
       " array([[1., 8., 6., 7., 1., 8., 1., 0.],\n",
       "        [2., 0., 6., 7., 8., 2., 2., 6.]])]"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a, (3,4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 拷贝和视图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  无拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:39:37.616305Z",
     "start_time": "2020-12-01T07:39:37.613798Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:39:40.369382Z",
     "start_time": "2020-12-01T07:39:40.365928Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:39:46.600472Z",
     "start_time": "2020-12-01T07:39:46.598201Z"
    }
   },
   "outputs": [],
   "source": [
    "b = a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:39:49.217477Z",
     "start_time": "2020-12-01T07:39:49.213905Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b is a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:40:18.312487Z",
     "start_time": "2020-12-01T07:40:18.310057Z"
    }
   },
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    print(id(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:40:22.241659Z",
     "start_time": "2020-12-01T07:40:22.238078Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4794714672"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T07:40:27.304869Z",
     "start_time": "2020-12-01T07:40:27.301848Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4794714672\n"
     ]
    }
   ],
   "source": [
    "f(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 视图或浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:03:06.366718Z",
     "start_time": "2020-12-01T08:03:06.364232Z"
    }
   },
   "outputs": [],
   "source": [
    "c = a.view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:03:09.887656Z",
     "start_time": "2020-12-01T08:03:09.884233Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c is a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:03:17.320106Z",
     "start_time": "2020-12-01T08:03:17.316425Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.base is a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:03:31.112012Z",
     "start_time": "2020-12-01T08:03:31.108627Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.flags.owndata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:03:53.103573Z",
     "start_time": "2020-12-01T08:03:53.100963Z"
    }
   },
   "outputs": [],
   "source": [
    "c = c.reshape((2,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:03:56.375958Z",
     "start_time": "2020-12-01T08:03:56.372483Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:05:10.598903Z",
     "start_time": "2020-12-01T08:05:10.596569Z"
    }
   },
   "outputs": [],
   "source": [
    "s = a[:, 1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:05:12.272884Z",
     "start_time": "2020-12-01T08:05:12.269070Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2],\n",
       "       [ 5,  6],\n",
       "       [ 9, 10]])"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:05:26.951033Z",
     "start_time": "2020-12-01T08:05:26.948565Z"
    }
   },
   "outputs": [],
   "source": [
    "s[:] = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:05:28.304577Z",
     "start_time": "2020-12-01T08:05:28.300728Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, 10, 10,  3],\n",
       "       [ 4, 10, 10,  7],\n",
       "       [ 8, 10, 10, 11]])"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:05:41.656293Z",
     "start_time": "2020-12-01T08:05:41.652657Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, 10, 10,  3,  4, 10],\n",
       "       [10,  7,  8, 10, 10, 11]])"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:05:53.350982Z",
     "start_time": "2020-12-01T08:05:53.348418Z"
    }
   },
   "outputs": [],
   "source": [
    "c[0,4] = 1234"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:05:54.735866Z",
     "start_time": "2020-12-01T08:05:54.732181Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   0,   10,   10,    3, 1234,   10],\n",
       "       [  10,    7,    8,   10,   10,   11]])"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:05:56.336097Z",
     "start_time": "2020-12-01T08:05:56.332447Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   0,   10,   10,    3],\n",
       "       [1234,   10,   10,    7],\n",
       "       [   8,   10,   10,   11]])"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:06:16.177028Z",
     "start_time": "2020-12-01T08:06:16.173499Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.base is a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 深拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:07:01.598757Z",
     "start_time": "2020-12-01T08:07:01.596531Z"
    }
   },
   "outputs": [],
   "source": [
    "d = a.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:07:03.904118Z",
     "start_time": "2020-12-01T08:07:03.900424Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d is a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:07:06.608111Z",
     "start_time": "2020-12-01T08:07:06.604519Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.base is a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:07:12.680410Z",
     "start_time": "2020-12-01T08:07:12.677910Z"
    }
   },
   "outputs": [],
   "source": [
    "d[0,0] = 9999"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:07:13.336576Z",
     "start_time": "2020-12-01T08:07:13.332743Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9999,   10,   10,    3],\n",
       "       [1234,   10,   10,    7],\n",
       "       [   8,   10,   10,   11]])"
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:07:14.072773Z",
     "start_time": "2020-12-01T08:07:14.068904Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   0,   10,   10,    3],\n",
       "       [1234,   10,   10,    7],\n",
       "       [   8,   10,   10,   11]])"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### copy通常在slicing之后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:08:19.700744Z",
     "start_time": "2020-12-01T08:08:19.300733Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.arange(int(1e8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:08:29.815184Z",
     "start_time": "2020-12-01T08:08:29.812653Z"
    }
   },
   "outputs": [],
   "source": [
    "b = a[:100].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:08:31.312324Z",
     "start_time": "2020-12-01T08:08:31.308425Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n",
       "       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n",
       "       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n",
       "       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n",
       "       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:08:33.512446Z",
     "start_time": "2020-12-01T08:08:33.508493Z"
    }
   },
   "outputs": [],
   "source": [
    "del a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 广播 broadcasting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=red>如果两个数组的后缘维度(从末尾开始算起的维度)的轴长度相符或其中一方的长度为1，则认为它们是广播兼容的。广播会在缺失维度和(或)轴长度为1的维度上进行。</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Figure1](https://numpy.org/devdocs/_images/theory.broadcast_1.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:15:15.535079Z",
     "start_time": "2020-12-01T08:15:15.532486Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.array([1.0,2.0,3.0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:14:57.295542Z",
     "start_time": "2020-12-01T08:14:57.293262Z"
    }
   },
   "outputs": [],
   "source": [
    "b = 2.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:15:17.432905Z",
     "start_time": "2020-12-01T08:15:17.429426Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 4., 6.])"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Figure2](https://numpy.org/devdocs/_images/theory.broadcast_2.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:36:36.125939Z",
     "start_time": "2020-12-01T08:36:36.123087Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.array([[ 0.0,  0.0,  0.0],\n",
    "...            [10.0, 10.0, 10.0],\n",
    "...            [20.0, 20.0, 20.0],\n",
    "...            [30.0, 30.0, 30.0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:37:27.021809Z",
     "start_time": "2020-12-01T08:37:27.019281Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.array([1.0, 2.0, 3.0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:37:28.926775Z",
     "start_time": "2020-12-01T08:37:28.923029Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  2.,  3.],\n",
       "       [11., 12., 13.],\n",
       "       [21., 22., 23.],\n",
       "       [31., 32., 33.]])"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a+b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![图3](https://numpy.org/devdocs/_images/theory.broadcast_3.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:37:35.518342Z",
     "start_time": "2020-12-01T08:37:35.515718Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.array([1.0, 2.0, 3.0, 4.0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:37:37.977428Z",
     "start_time": "2020-12-01T08:37:37.970828Z"
    }
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,3) (4,) ",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-210-ca730b97bf8a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,3) (4,) "
     ]
    }
   ],
   "source": [
    "a+b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:39:22.905626Z",
     "start_time": "2020-12-01T08:39:22.770709Z"
    }
   },
   "source": [
    "![图4](https://numpy.org/devdocs/_images/theory.broadcast_4.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:39:42.669606Z",
     "start_time": "2020-12-01T08:39:42.666715Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.array([0.0, 10.0, 20.0, 30.0]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:41:32.358099Z",
     "start_time": "2020-12-01T08:41:32.354099Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  2.,  3.,  4.],\n",
       "       [11., 12., 13., 14.],\n",
       "       [21., 22., 23., 24.],\n",
       "       [31., 32., 33., 34.]])"
      ]
     },
     "execution_count": 216,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:, newaxis] + b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![图5](https://images2015.cnblogs.com/blog/1081032/201707/1081032-20170706112325347-1466943324.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 高级索引技巧"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Indexing with Arrays of Indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:45:00.022026Z",
     "start_time": "2020-12-01T08:45:00.019454Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.arange(12) ** 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:45:48.872890Z",
     "start_time": "2020-12-01T08:45:48.869060Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0,   1,   4,   9,  16,  25,  36,  49,  64,  81, 100, 121])"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:45:41.093640Z",
     "start_time": "2020-12-01T08:45:41.091101Z"
    }
   },
   "outputs": [],
   "source": [
    "i = np.array([1,2,3,8,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:45:44.470579Z",
     "start_time": "2020-12-01T08:45:44.466767Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  9, 64, 25])"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:46:19.232641Z",
     "start_time": "2020-12-01T08:46:19.229844Z"
    }
   },
   "outputs": [],
   "source": [
    "j = np.array([[3,4], [9,7]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:46:20.462805Z",
     "start_time": "2020-12-01T08:46:20.458951Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4],\n",
       "       [9, 7]])"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:46:24.494826Z",
     "start_time": "2020-12-01T08:46:24.490981Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 9, 16],\n",
       "       [81, 49]])"
      ]
     },
     "execution_count": 224,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T08:53:14.710763Z",
     "start_time": "2020-12-01T08:53:14.707666Z"
    }
   },
   "outputs": [],
   "source": [
    "palette = np.array([[0, 0, 0],         # black\n",
    "                    [255, 0, 0],       # red\n",
    "                    [0, 255, 0],       # green\n",
    "                    [0, 0, 255],       # blue\n",
    "                    [255, 255, 255]])  # white"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:02:05.598112Z",
     "start_time": "2020-12-01T09:02:05.595061Z"
    }
   },
   "outputs": [],
   "source": [
    "image = np.array([[0, 1, 2, 4],        # each value corresponds to a color in the palette\n",
    "                  [0, 3, 4, 0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:02:07.406488Z",
     "start_time": "2020-12-01T09:02:07.402425Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[  0,   0,   0],\n",
       "        [255,   0,   0],\n",
       "        [  0, 255,   0],\n",
       "        [255, 255, 255]],\n",
       "\n",
       "       [[  0,   0,   0],\n",
       "        [  0,   0, 255],\n",
       "        [255, 255, 255],\n",
       "        [  0,   0,   0]]])"
      ]
     },
     "execution_count": 233,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "palette[image] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T12:56:35.135385Z",
     "start_time": "2020-12-02T12:56:35.130310Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T12:56:35.757435Z",
     "start_time": "2020-12-02T12:56:35.745092Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:20:58.931730Z",
     "start_time": "2020-12-01T09:20:58.928916Z"
    }
   },
   "outputs": [],
   "source": [
    "i = np.array([[0, 1],                     # indices for the first dim of a\n",
    "              [1, 2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:20:59.697320Z",
     "start_time": "2020-12-01T09:20:59.694687Z"
    }
   },
   "outputs": [],
   "source": [
    "j = np.array([[2, 1],                     # indices for the second dim\n",
    "              [3, 3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:21:00.578872Z",
     "start_time": "2020-12-01T09:21:00.574371Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  6],\n",
       "       [ 6, 10]])"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[i,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:21:02.098933Z",
     "start_time": "2020-12-01T09:21:02.094746Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 2,  1],\n",
       "        [ 3,  3]],\n",
       "\n",
       "       [[ 6,  5],\n",
       "        [ 7,  7]],\n",
       "\n",
       "       [[10,  9],\n",
       "        [11, 11]]])"
      ]
     },
     "execution_count": 253,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:, j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:23:01.283546Z",
     "start_time": "2020-12-01T09:23:01.281072Z"
    }
   },
   "outputs": [],
   "source": [
    "l =(i,j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:23:02.181285Z",
     "start_time": "2020-12-01T09:23:02.177019Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[0, 1],\n",
       "        [1, 2]]),\n",
       " array([[2, 1],\n",
       "        [3, 3]]))"
      ]
     },
     "execution_count": 255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:23:07.085322Z",
     "start_time": "2020-12-01T09:23:07.081307Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  5],\n",
       "       [ 7, 11]])"
      ]
     },
     "execution_count": 256,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[l]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:26:25.092089Z",
     "start_time": "2020-12-01T09:26:25.089281Z"
    }
   },
   "outputs": [],
   "source": [
    "s = np.array([i, j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:26:27.791717Z",
     "start_time": "2020-12-01T09:26:27.783509Z"
    }
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "index 3 is out of bounds for axis 0 with size 3",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-258-c057dc68e5fe>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: index 3 is out of bounds for axis 0 with size 3"
     ]
    }
   ],
   "source": [
    "a[s]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:26:45.636275Z",
     "start_time": "2020-12-01T09:26:45.632381Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  5],\n",
       "       [ 7, 11]])"
      ]
     },
     "execution_count": 259,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[tuple(s)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T12:56:55.962990Z",
     "start_time": "2020-12-02T12:56:55.958176Z"
    }
   },
   "outputs": [],
   "source": [
    "time = np.linspace(20, 145, 5)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T12:56:56.534734Z",
     "start_time": "2020-12-02T12:56:56.528598Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 20.  ,  51.25,  82.5 , 113.75, 145.  ])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T12:57:00.751944Z",
     "start_time": "2020-12-02T12:57:00.747564Z"
    }
   },
   "outputs": [],
   "source": [
    "data = np.sin(np.arange(20)).reshape(5,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T12:57:02.426763Z",
     "start_time": "2020-12-02T12:57:02.420586Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.        ,  0.84147098,  0.90929743,  0.14112001],\n",
       "       [-0.7568025 , -0.95892427, -0.2794155 ,  0.6569866 ],\n",
       "       [ 0.98935825,  0.41211849, -0.54402111, -0.99999021],\n",
       "       [-0.53657292,  0.42016704,  0.99060736,  0.65028784],\n",
       "       [-0.28790332, -0.96139749, -0.75098725,  0.14987721]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T12:57:30.627486Z",
     "start_time": "2020-12-02T12:57:30.623549Z"
    }
   },
   "outputs": [],
   "source": [
    "ind = data.argmax(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T12:57:33.845335Z",
     "start_time": "2020-12-02T12:57:33.842264Z"
    }
   },
   "outputs": [],
   "source": [
    "time_max = time[ind]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:00:18.989980Z",
     "start_time": "2020-12-02T13:00:18.982875Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 82.5 ,  20.  , 113.75,  51.25])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time_max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:00:23.826997Z",
     "start_time": "2020-12-02T13:00:23.822533Z"
    }
   },
   "outputs": [],
   "source": [
    "data_max = data[ind, range(data.shape[1])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 290,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-01T09:34:32.850649Z",
     "start_time": "2020-12-01T09:34:32.846666Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 290,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(data_max == data.max(axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:00:57.237380Z",
     "start_time": "2020-12-02T13:00:57.220444Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.98935825,  0.41211849, -0.54402111, -0.99999021],\n",
       "       [ 0.        ,  0.84147098,  0.90929743,  0.14112001],\n",
       "       [-0.53657292,  0.42016704,  0.99060736,  0.65028784],\n",
       "       [-0.7568025 , -0.95892427, -0.2794155 ,  0.6569866 ]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[ind]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:00:57.935151Z",
     "start_time": "2020-12-02T13:00:57.928403Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9893582466233818"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[ind[0],0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:10:45.586540Z",
     "start_time": "2020-12-02T13:10:45.582979Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.arange(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:07:22.159667Z",
     "start_time": "2020-12-02T13:07:22.155243Z"
    }
   },
   "outputs": [],
   "source": [
    "a[[1,3,4]] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:07:49.118778Z",
     "start_time": "2020-12-02T13:07:49.114870Z"
    }
   },
   "outputs": [],
   "source": [
    "a[[0,0,2]]=[1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:10:56.412389Z",
     "start_time": "2020-12-02T13:10:56.407695Z"
    }
   },
   "outputs": [],
   "source": [
    "a[[0,0,2]] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:10:58.059613Z",
     "start_time": "2020-12-02T13:10:58.054299Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 3, 3, 4])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Indexing with Boolean Arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:12:46.695439Z",
     "start_time": "2020-12-02T13:12:46.691676Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:12:49.569291Z",
     "start_time": "2020-12-02T13:12:49.563064Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:12:52.903192Z",
     "start_time": "2020-12-02T13:12:52.899732Z"
    }
   },
   "outputs": [],
   "source": [
    "b = a > 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:12:53.829048Z",
     "start_time": "2020-12-02T13:12:53.822573Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False],\n",
       "       [False,  True,  True,  True],\n",
       "       [ True,  True,  True,  True]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:13:11.499509Z",
     "start_time": "2020-12-02T13:13:11.493282Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[b]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:13:22.468632Z",
     "start_time": "2020-12-02T13:13:22.464277Z"
    }
   },
   "outputs": [],
   "source": [
    "a[b] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:13:23.362228Z",
     "start_time": "2020-12-02T13:13:23.356078Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3],\n",
       "       [4, 0, 0, 0],\n",
       "       [0, 0, 0, 0]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:16:02.822057Z",
     "start_time": "2020-12-02T13:16:02.817445Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.arange(12).reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:16:04.352522Z",
     "start_time": "2020-12-02T13:16:04.342301Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:16:18.950070Z",
     "start_time": "2020-12-02T13:16:18.944358Z"
    }
   },
   "outputs": [],
   "source": [
    "b1 = np.array([False,True,True])   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:16:34.912431Z",
     "start_time": "2020-12-02T13:16:34.899276Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[b1, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:16:44.623606Z",
     "start_time": "2020-12-02T13:16:44.615496Z"
    }
   },
   "outputs": [],
   "source": [
    "b2 = np.array([True,False,True,False])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:16:57.659187Z",
     "start_time": "2020-12-02T13:16:57.653430Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2],\n",
       "       [ 4,  6],\n",
       "       [ 8, 10]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:,b2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:27:55.908964Z",
     "start_time": "2020-12-02T13:27:55.902526Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4, 10])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[b1, b2]  # a weird thing to do"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  The ix_() function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:31:48.391529Z",
     "start_time": "2020-12-02T13:31:48.386761Z"
    }
   },
   "outputs": [],
   "source": [
    "a = np.array([2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:31:53.214566Z",
     "start_time": "2020-12-02T13:31:53.203449Z"
    }
   },
   "outputs": [],
   "source": [
    "b = np.array([8,5,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:31:58.862421Z",
     "start_time": "2020-12-02T13:31:58.858007Z"
    }
   },
   "outputs": [],
   "source": [
    "c = np.array([5,4,6,8,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:32:04.939350Z",
     "start_time": "2020-12-02T13:32:04.935416Z"
    }
   },
   "outputs": [],
   "source": [
    "ax,bx,cx = np.ix_(a,b,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:35:46.448540Z",
     "start_time": "2020-12-02T13:35:46.441439Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[2]],\n",
       "\n",
       "       [[3]],\n",
       "\n",
       "       [[4]],\n",
       "\n",
       "       [[5]]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:35:49.070045Z",
     "start_time": "2020-12-02T13:35:49.057716Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[8],\n",
       "        [5],\n",
       "        [4]]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:35:59.376343Z",
     "start_time": "2020-12-02T13:35:59.369738Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[5, 4, 6, 8, 3]]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:36:13.977618Z",
     "start_time": "2020-12-02T13:36:13.973985Z"
    }
   },
   "outputs": [],
   "source": [
    "a=np.arange(10).reshape(2,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:36:22.106510Z",
     "start_time": "2020-12-02T13:36:22.101185Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:36:58.520781Z",
     "start_time": "2020-12-02T13:36:58.517193Z"
    }
   },
   "outputs": [],
   "source": [
    "a[np.ix_([0,1],[2,3])]=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:36:59.750419Z",
     "start_time": "2020-12-02T13:36:59.743635Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 2, 4],\n",
       "       [5, 6, 2, 2, 9]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-02T13:38:57.161572Z",
     "start_time": "2020-12-02T13:38:57.150624Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[0],\n",
       "        [1]]),\n",
       " array([[2, 3]]))"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ix_([0,1],[2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:py37] *",
   "language": "python",
   "name": "conda-env-py37-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
